<template>
	<view class="login-page">
		<view class="login-logo">
			<u--image src="../../static/img/logo-login.png" width="150" height="49"></u--image>
		</view>
		<u-button text="登录 / 注册" size="large" shape="circle" color="#000" @click="toPhoneLogin">
		</u-button>
		<view class="login-checkbox flex-no-horizontal">
			<view style="margin-right: 10rpx;" @click="ifAgreement = !ifAgreement">
				<u-icon :name="ifAgreement ? 'checkmark-circle-fill' : 'checkmark-circle' " :color="ifAgreement ? '#000' : '#818181'  " size="20"></u-icon>
			</view>
			<view>
				<text>我已阅读并同意</text> 
				<text @click="toProtocol(3)">《轻松小镇用户协议》</text>
				<text @click="toProtocol(1)">《轻松小镇隐私政策》</text>
				<text @click="toProtocol(5)">《虚拟商品购买服务协议》</text>
			</view>
		</view>
		
		<view class="flex-no-horizontal" style="justify-content: center;">
			<view class="login-box-bottmom flex-no-horizontal">
				
				<text class="interval"></text>
				
				<!-- <view class="login-btn" @click="loginFun('wx')">
					<view class="login-btn-icon flex-no-horizontal">
						<u-icon name="weixin-fill" color="#FFF" size="28"></u-icon>
					</view>
				</view> -->
				
				<view class="login-btn" @click="loginFun('appleid')" v-if="type !== 'Android'">
					<view class="login-btn-icon flex-no-horizontal">
						<u-icon name="apple-fill" color="#FFF" size="28"></u-icon>
					</view>
				</view>
				
				<text class="interval"></text>
			</view>
		</view>

	</view>
</template>

<script>
	import {userLoginApi} from '../../api/index.js'
	import { uniLogin,uniUserInfo,uniAppleLogin } from '../../utility/weChatLogin.js'
	import { mapMutations } from 'vuex'
	export default {
		data() {
			return {
				unionId:'',
				appleId:'',
				ifAgreement:false,
				type:'',
				loginType:'',
			}
		},
		onLoad() {
			//#ifdef APP-PLUS
			this.type = plus.os.name
			//#endif
		},
		methods: {
			...mapMutations(['setUserInfo','setUserToken']),
			
			
			//登录
			async loginFun(type){
				try{
					if(!this.ifAgreement){
						uni.showToast({
							title:'请先同意协议',
							icon:'none'
						})
						return
					}
					uni.showLoading({
						title: '正在登录',
						mask:true
					})
					setTimeout(()=> {
						uni.hideLoading()
					}, 5000);
					let data = {}
					if(type === 'wx'){
						//唤起微信登录
						const wechatRes = await uniLogin()
						//获取用户信息
						const infoRes = await uniUserInfo()
						this.unionId = infoRes.userInfo.unionId
						data = {
							unionId:infoRes.userInfo.unionId,
						}
						this.appleId = ''
					}else{
						//调起苹果登录
						const AppleRes = await uniAppleLogin()
						this.appleId = AppleRes.authResult.openid
						data = {
							appleId:AppleRes.authResult.openid,
						}
						this.unionId = ''
					}
					//登录请求
					console.log(444,{...data, loginType:type})
					const loginRes = await userLoginApi({
						...data,
						loginType:type
					})
					
					//缓存用户信息
					this.setUserInfo(loginRes.data.userInfo)
					this.setUserToken('Bearer ' + loginRes.data.token)
					uni.showToast({
						title: '登录成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '../ring/index'
						})
					}, 1000);
					
					
				}catch(e){
					uni.hideLoading()
					uni.showToast({
						title:e.message || e.data,
						icon:'none'
					})
					switch(e.code) {
					    case 100701:
							//未注册
							uni.navigateTo({
								url:'./phoneLogin?unionId=' + this.unionId + '&appleId=' + this.appleId
							})
					        break;
					    case 100702:
					        //未绑定手机号
							uni.navigateTo({
								url:'./reg?unionId=' + this.unionId + '&appleId=' + this.appleId
							})
					        break;
					    case 100703:
					        //未绑定微信号
					        break;
					} 
				}
			},
			
			//跳转手机登录
			toPhoneLogin(){
				if(!this.ifAgreement){
					uni.showToast({
						title:'请先同意协议',
						icon:'none'
					})
					return
				}
				uni.navigateTo({
					url:'./phoneLogin'
				})
			},
			
			// 跳转各种协议
			toProtocol (id) {
				console.log(id)
				uni.navigateTo({
					url: `./BuiltInWebPage?type=${id}`
				})
			}
		},
		
	}
</script>

<style lang="scss" scoped>
	.login-page {
		padding: 0 40rpx;

		.login-logo {
			height: 893rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.login-checkbox {
			margin: 51rpx 0 166rpx;
			.round {
				display: inline-block;
				width: 24rpx;
				height: 24rpx;
				border: 1rpx solid #818181;
				border-radius: 50%;
				margin-right: 18rpx;
			}

			text:nth-of-type(1) {
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #808080;
			}

			text:nth-of-type(2),
			text:nth-of-type(3),
			text:nth-of-type(4) {
				font-size: 26rpx;
			}
		}
		.login-box-bottmom{
			width: 100%;
			justify-content: space-around;
			.interval{
				width: 160rpx;
				height: 2rpx;
				background-color: #F0F0F0;
			}
			
			.login-btn{
				text-align: center;
				.login-btn-title{
					margin-top: 10rpx;
					color: #7F7F7F;
					font-size: $uni-font-size-base;
				}
				
				.login-btn-icon{
					margin: 0 auto;
					justify-content: center;
					width: 104rpx;
					height: 104rpx;
					background-color: $uni-color-black;
					border-radius: $uni-border-radius-circle;
					image{
						width: 56rpx;
						height: 56rpx;
					}
					
				}
			}
		}

	}
</style>
